#include "map.h"
//内存操作
#define ASM_TYPE_BYTE "BYTE"
#define ASM_TYPE_WORD "WORD"
#define ASM_TYPE_DWORD "DWORD"
#define ASM_TYPE_QWORD "QWORD"
//数据定义
#define ASM_SIZE_DB "DB"
#define ASM_SIZE_DW "DW"
#define ASM_SIZE_DD "DD"
#define ASM_SIZE_DQ "DQ"
//reg
#define ASM_AL  "al"
#define ASM_AX  "ax"
#define ASM_EAX "eax"
#define ASM_RAX "rax"
#define ASM_DL  "dl"
#define ASM_DX  "dx"
#define ASM_EDX "edx"
#define ASM_RDX "rdx"

//数值数据类型边界
#define NUM_MAX     0X7f
#define NUM_MIN     0X80
#define NUM2_MAX    0x7fff
#define NUM2_MIN    0x8000
#define NUM4_MAX    0x7fffffff
#define NUM4_MIN    0x80000000
#define NUM8_MAX    0x7fffffffffffffff
#define NUM8_MIN    0x8000000000000000
typedef enum {
    REG_NULL,
    REG_USE_A,
    REG_USE_D,
    REG_USE_A_MANY,
} RegState; // 寄存器状态

struct reg_use_info
{
    RegState state;
    exprType action;
    OperationType o_t;
    variable *left;
    variable *right;

};
typedef struct _asm_float_tag_
{
    Num4    No;
    DataType   type;
    F64     *f8;
    F32     *f4;
    Str     string;
    struct _asm_float_tag_ * next;

}AsmFloatTAG;

//函数调用数据
typedef struct _func_params_stack_{
    char is_first;
    char is_func_params_parser;
    char set_float_reg;
    char set_no_float_reg;
    Num4 set_stack_mem;
    struct _func_params_stack_ *next;
}FuncParamsStack;

//每条语句解析信息
typedef struct _run_statement_info_{
    char is_first;
    DataType result_type;
}RunStatementInfo;
#define check_number(type) \
  (type != DATA_TYPE_NUMBER_1 && type != DATA_TYPE_NUMBER_2\
   && type != DATA_TYPE_NUMBER_4 && type != DATA_TYPE_NUMBER_8 )

#define is_number(type) \
  (type == DATA_TYPE_NUMBER_1 || type == DATA_TYPE_NUMBER_2\
   || type == DATA_TYPE_NUMBER_4 || type == DATA_TYPE_NUMBER_8 )

#define is_float(type)  (type == DATA_TYPE_F_NUMBER_4 || type == DATA_TYPE_F_NUMBER_8 )

#define is_no_float(type)  (is_number(type) || type==DATA_TYPE_STRING)

#define inc_sp(sp, type)  if(type==DATA_TYPE_NUMBER_1){ \
    sp +=1;\
}else if(type==DATA_TYPE_NUMBER_2){ \
    sp +=2;\
}else if(type==DATA_TYPE_NUMBER_4||type==DATA_TYPE_F_NUMBER_4){ \
    sp +=4;\
}else if(type==DATA_TYPE_NUMBER_8||type==DATA_TYPE_F_NUMBER_8){ \
    sp +=8;\
}

#define sub_stack(sp, type)  if(type==DATA_TYPE_NUMBER_1){ \
    sp -=1;\
}else if(type==DATA_TYPE_NUMBER_2){ \
    sp -=2;\
}else if(type==DATA_TYPE_NUMBER_4||type==DATA_TYPE_F_NUMBER_4){ \
    sp -=4;\
}else if(type==DATA_TYPE_NUMBER_8||type==DATA_TYPE_F_NUMBER_8){ \
    sp -=8;\
}

#define _string(s) #s

#define dis_expr_code 0 //是否显示表达式code

#define CheckNumber(t) (t==DATA_TYPE_NUMBER_1||t==DATA_TYPE_NUMBER_2 \
||t==DATA_TYPE_NUMBER_4||t==DATA_TYPE_NUMBER_8||t==DATA_TYPE_F_NUMBER_4||t==DATA_TYPE_F_NUMBER_8)
/*
#define _ASM_OP(opcode, reg1, reg2)  opcode reg1, reg2
#define _ASM_OP2(opcode,dt, reg1, reg2) opcode BYTE dt reg1, reg2
#define _ASM_OP3(opcode,dt, reg1, reg2) opcode reg1, BYTE dt reg2
*/
variable *ParserStatement(treeTable *statement, VarList *var_list);
variable *RunStatement(treeTable *statement, VarList *var_list);
variable *RunTree(tree *expr, VarList *var_list);
char * GetAsmSize(DataType type);
variable *CopyVariable(variable *source);
void PutAsm(char *al);
void InsAFL(variable *var, VarAttr va);
void WriteAFL();
void evaluation(variable *source, variable *target);

Num8 GetNumber(variable *source);
treeTable * GetLsatTree(treeTable * head);